#
#    Copyright (C) 2016 Francisco Javier Parra <franparpe@openmailbox.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

import math
import numpy
from qtcam.core.UnitChanger import Radiants2Degrees
from qtcam.core.parameters import SectionParameters


class CicloidalSection(SectionParameters):
    """This objects represents the cicloidal section of a cam

    """
    def __init__(self, LowestPosition=0.0,
                 HighestPosition=50.0, StartAngle=0.0,
                 EndAngle=100.0, Precision=0.1, AngularVelocity=1):
        "Initializes all necessary paramaters for further calculation"
        super(CicloidalSection, self).__init__(LowestPosition,
                                               HighestPosition,
                                               StartAngle,
                                               EndAngle,
                                               Precision,
                                               AngularVelocity)

    def Position(self, currentAngle):
        return (self.LowestPosition + self.HighestPosition *
                ((currentAngle / self.EndAngle) - (1 / (2 *
                                                        math.pi)) *
                 math.sin((2 * math.pi * currentAngle) / self.EndAngle)))

    def Velocity(self, currentAngle):
        """This method returns a the cicloidal velocity if
        the an angle is given (useful for specifi values)

        Arguments:
        - `self`:
        - `currentAngle`:
        """
        angularVelocityDeg = Radiants2Degrees(self.AngularVelocity)
        return (angularVelocityDeg * (self.HighestPosition / self.EndAngle) *
                (1 - math.cos((2 * math.pi * currentAngle) / self.EndAngle)))

    def Acceleration(self, currentAngle):
        """This method returns the acceleration for a given angle

        Arguments:
        - `self`:
        - `currentAngle`:
        """
        angularVelocityDeg = Radiants2Degrees(self.AngularVelocity)
        return (math.pow(angularVelocityDeg, 2) *
                (2 * math.pi * self.HighestPosition /
                 math.pow(self.EndAngle, 2))
                * math.sin(((2 * math.pi * currentAngle) / self.EndAngle)))
    
    def OverAcceleration(self, currentAngle):
        """This method returns the derivative of the acceleration

        Arguments:
        - `self`:
        - `currentAngle`:
        """
        angularVelocityDeg = Radiants2Degrees(self.AngularVelocity)
        return (math.pow(angularVelocityDeg, 3) *
                ((4 * math.pow(math.pi, 2) * self.HighestPosition) /
                 math.pow(self.EndAngle, 3)) *
                math.cos((2 * math.pi * currentAngle) / (self.EndAngle)))

    def ArrayPosition(self):
        """This method creates an array that will go later
        on the ordinate axis
        
        Arguments:
        - `self`:
        """
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle

        return (self.LowestPosition + self.HighestPosition *
                ((theta / beta) - ((1 / (2 * numpy.pi)) *
                 numpy.sin((2 * numpy.pi * theta) / beta))))

    def ArrayVelocity(self):
        """This method creates an array that represents all values of the Velocity
        that will go on the ordiante axis

        Arguments:
        - `self`:
        """
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return (self.AngularVelocity * (self.HighestPosition / math.radians(beta)) *
                (1 - numpy.cos((2 * math.pi * theta)
                               / beta)))

    def ArrayAcceleration(self):
        """This method creates an array with the values of the Acceleration
        that will go on the ordiante axis

        Arguments:
        - `self`:
        """
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return (numpy.power(self.AngularVelocity, 2) *
                (2 * math.pi * self.HighestPosition /
                 numpy.power(math.radians(beta), 2))
                * numpy.sin(((2 * numpy.pi * theta) / beta)))

    def ArrayOverAcceleration(self):
        """This method creates an array with the values of the derivative
        of the Acceleration

        Arguments:
        - `self`:
        """
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return (numpy.power(self.AngularVelocity, 3) *
                ((4 * numpy.power(math.pi, 2) * self.HighestPosition) /
                 numpy.power(math.radians(beta), 3)) *
                numpy.cos((2 * numpy.pi * theta)
                          / beta))
